<template>
  <div class="news-detail-page">
    <Header />
    <main class="page-content">
      <div class="container">
        <div v-if="error" class="error">{{ error }}</div>
        <div v-else-if="detailData" class="detail-content">
          <div class="detail-header">
            <h1 class="page-title">{{ detailData.title }}</h1>
            <p class="detail-date">{{ formatDate(detailData.content_date) }}</p>
          </div>
          
          <div class="detail-body" v-html="detailData.content"></div>
          
          <!-- Pagination -->
          <div class="pagination">
            <a 
              v-if="prevId"
              :href="`/news/detail?type=news&id=${prevId}`"
              class="pagination-link" 
              @click.prevent="goToDetail(prevId)"
            >
              « Previous
            </a>
            <span v-else class="pagination-disabled">« Previous</span>
            
            <a 
              v-if="nextId"
              :href="`/news/detail?type=news&id=${nextId}`"
              class="pagination-link" 
              @click.prevent="goToDetail(nextId)"
            >
              Next »
            </a>
            <span v-else class="pagination-disabled">Next »</span>
          </div>
        </div>
      </div>
    </main>
    <Footer />
  </div>
</template>

<script>
import Header from '../components/Header.vue'
import Footer from '../components/Footer.vue'
import { getNewsDetail, getNewsList, API_BASE_URL } from '@/utils/api'

export default {
  name: 'NewsDetail',
  components: {
    Header,
    Footer
  },
  data() {
    return {
      detailData: null,
      loading: false,
      error: null,
      currentId: null,
      prevId: null,
      nextId: null,
      allIds: []
    }
  },
  async mounted() {
    await this.loadDetail()
  },
  watch: {
    '$route'(to, from) {
      if (to.query.id !== from.query.id) {
        this.loadDetail()
      }
    }
  },
  methods: {
    /**
     * 加载详情数据
     */
    async loadDetail() {
      const contentId = parseInt(this.$route.query.id)
      if (!contentId) {
        this.error = '无效的内容ID'
        return
      }
      
      this.currentId = contentId
      this.loading = true
      this.error = null
      
      try {
        // 获取详情数据
        this.detailData = await getNewsDetail(contentId)
        
        // 获取列表数据以支持翻页
        await this.loadListForPagination()
      } catch (error) {
        console.error('获取News详情失败:', error)
        this.error = error.message || '获取新闻详情失败'
      } finally {
        this.loading = false
      }
    },
    
    /**
     * 加载列表数据以支持翻页功能
     */
    async loadListForPagination() {
      try {
        // 获取所有列表数据（使用较大的page_size）
        const data = await getNewsList(1, 1000)
        this.allIds = (data.list || []).map(item => item.content_id)
        
        // 找到当前项的位置
        const currentIndex = this.allIds.indexOf(this.currentId)
        
        if (currentIndex > 0) {
          this.prevId = this.allIds[currentIndex - 1]
        } else {
          this.prevId = null
        }
        
        if (currentIndex < this.allIds.length - 1) {
          this.nextId = this.allIds[currentIndex + 1]
        } else {
          this.nextId = null
        }
      } catch (error) {
        console.error('获取News列表失败:', error)
        // 如果获取列表失败，不影响详情页显示，只是没有翻页功能
      }
    },
    
    /**
     * 跳转到详情页（当前标签页打开）
     */
    goToDetail(contentId) {
      this.$router.push(`/news/detail?type=news&id=${contentId}`)
    },
    
    /**
     * 格式化日期
     */
    formatDate(dateString) {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleDateString('en-US', { 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric' 
      })
    },
    
    /**
     * 获取图片完整URL
     */
    getImageUrl(imageUrl) {
      if (!imageUrl) return ''
      if (imageUrl.startsWith('http')) {
        return imageUrl
      }
      return `${API_BASE_URL}${imageUrl}`
    }
  }
}
</script>

<style scoped>
.news-detail-page .container {
  max-width: 1200px;
}

.detail-content {
  margin-top: 40px;
}

.detail-header {
  text-align: center;
  margin-bottom: 40px;
}

.detail-header .page-title {
  font-size: 30px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 3px solid var(--primary-color);
}

.detail-date {
  color: #999;
  font-size: 0.9em;
  margin: 0;
}

.detail-body {
  color: var(--text-color);
  line-height: 1.8;
  font-size: 1em;
  margin: 40px 0;
}

.detail-body :deep(p) {
  margin: 0 0 15px 0;
}

.detail-body :deep(img) {
  max-width: 100%;
  height: auto;
  margin: 20px 0;
  border-radius: 8px;
}

.detail-body :deep(h1),
.detail-body :deep(h2),
.detail-body :deep(h3),
.detail-body :deep(h4) {
  color: #333;
  margin: 25px 0 15px 0;
  font-weight: 600;
}

.detail-body :deep(h1) {
  font-size: 1.8em;
}

.detail-body :deep(h2) {
  font-size: 1.5em;
}

.detail-body :deep(h3) {
  font-size: 1.3em;
}

.detail-body :deep(ul),
.detail-body :deep(ol) {
  margin: 15px 0;
  padding-left: 30px;
}

.detail-body :deep(li) {
  margin: 8px 0;
}

.detail-body :deep(a) {
  color: var(--primary-color);
  text-decoration: none;
}

.detail-body :deep(a:hover) {
  color: var(--hover-color);
  text-decoration: underline;
}

/* Pagination Styles */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 50px;
  padding-top: 30px;
  border-top: 1px solid var(--border-color);
}

.pagination-link {
  color: var(--primary-color);
  font-weight: 500;
  font-size: 0.95em;
  text-decoration: none;
  transition: all 0.3s ease;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 3px;
}

.pagination-link:hover {
  color: white;
  background-color: var(--primary-color);
  text-decoration: none;
}

.pagination-disabled {
  color: #ccc;
  font-size: 0.95em;
  cursor: default;
  padding: 5px 10px;
}

.loading {
  text-align: center;
  padding: 40px 0;
  color: #666;
  font-size: 1em;
}

.error {
  text-align: center;
  padding: 40px 0;
  color: #e74c3c;
  font-size: 1em;
}

/* Responsive Design */
@media (max-width: 768px) {
  .detail-body {
    font-size: 0.95em;
  }
  
  .pagination {
    flex-wrap: wrap;
    gap: 15px;
  }
  
  .pagination-link {
    flex: 1;
    text-align: center;
  }
}
</style>

